package cn.onlineclass.common;

import cn.onlineclass.entity.OCollect;
import cn.onlineclass.service.CollectService;
import cn.onlineclass.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class CollectController {

    //商品分页查询
    public void search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        int pageSize = StringUtil.toInt(request.getParameter("pagaSize"),10);
        String openid = request.getParameter("openid");
        AjaxPage search = CollectService.COS.search(pageNo,pageSize,openid);
        String str = JSON.toJSONStringWithDateFormat(search,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }

    //增加商品
    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String openid = request.getParameter("openid");
//        Date createtime = StringUtil.parse(request.getParameter("createtime"));
//        Date modifytime = StringUtil.parse(request.getParameter("modifytime"));
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        int videoid = StringUtil.toInt(request.getParameter("videoid"),0);
        OCollect product =new OCollect(openid,null,null,inuse,videoid);
        AjaxResult add = CollectService.COS.add(product);
        String str = JSON.toJSONString(add);
        response.getWriter().print(str);
       return add;
    }

    //修改
    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));
        String openid = request.getParameter("openid");
        Date createtime = StringUtil.parse(request.getParameter("createtime"));
        Date modifytime = StringUtil.parse(request.getParameter("modifytime"));
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        int videoid = StringUtil.toInt(request.getParameter("video_id"),0);
        OCollect co = new OCollect(id,openid,createtime,modifytime,inuse,videoid);
        AjaxResult update = CollectService.COS.update(co);
        String str = JSON.toJSONString(update);
        response.getWriter().print(str);
        return update;
    }

    public void toupdate(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));
        CollectService mt = new CollectService();
        OCollect m = mt.get(id);
        String str = JSON.toJSONStringWithDateFormat(m,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }
//
//    public void toupdate1(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        String str = JSON.toJSONStringWithDateFormat(c,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
//        response.getWriter().print(str);
//    }

    //删除
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        AjaxResult del = CollectService.COS.del(id);
        String str = JSON.toJSONString(del);
        response.getWriter().print(str);
        return del;
    }

}
